﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Collections;
using Reborn_Server.App1AppHandler;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Potion_Mana : Buff
    {
        public Potion_Mana()
        {
            spellTimer.Interval = 750;
            minutesLeft = -2;
            ticksLeft = 20;
        }

        public void Consume(ref Character myCharacter, Loot lootitem)
        {
            TargetCreatureBase = myCharacter;

            LockItem lockItem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockItem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                List<Character> CharactersWithin4 = new List<Character>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 8 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 8)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                            {
                                Character character = (Character)creaturebase;
                                character.PlaySound("DrinkBottle", myCharacter.MapX, myCharacter.MapY);
                            }
                        }
                    }
                    if (Math.Abs(mapitem.MapX - myCharacter.MapX) <= 4 && Math.Abs(mapitem.MapY - myCharacter.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                                CharactersWithin4.Add((Character)creaturebase);
                        }
                    }
                }

                if (TargetCreatureBase.Mana == TargetCreatureBase.MaxMana)
                    return;

                SpellIcon potion = new SpellIcon();
                this.SpellID = TargetCreatureBase.GameHandler.ItemIDGen.GetSpellID();
                potion.ClassName = lootitem.ClassName;
                potion.Description = lootitem.Description;
                potion.ImageURL = lootitem.ImageURL;
                potion.Name = lootitem.Name;
                potion.Offensive = false;
                potion.CastType = "Self";

                this.spellIcon = potion;

                //Remove existing heal potions
                foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
                {
                    if (buff.spellIcon.Name == base.spellIcon.Name)
                    {
                        buff.RemoveSpell(false, false);
                    }
                }
                TargetCreatureBase.buffManager.AddBuff(this);
                TargetCreatureBase.buffManager.UpdateBuffs();

                foreach (Character character in CharactersWithin4)
                    character.AddCreatureBase(TargetCreatureBase);
            }
            spellTimer.Start();
        }
        override protected void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            ticksLeft--;

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (TargetCreatureBase.Mana >= TargetCreatureBase.MaxMana || TargetCreatureBase.HP < 1)
                {
                    RemoveSpell(true, false);
                    return;
                }

                TargetCreatureBase.Mana = TargetCreatureBase.Mana + 50;
                TargetCreatureBase.Update_Character();
                if (TargetCreatureBase.Mana >= TargetCreatureBase.MaxMana)
                {
                    ticksLeft = 0;
                }

                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if ((mapitem.MapX >= TargetCreatureBase.MapX - 4 && mapitem.MapX <= TargetCreatureBase.MapX + 4) && (mapitem.MapY >= TargetCreatureBase.MapY - 4 && mapitem.MapY <= TargetCreatureBase.MapY + 4))
                    {
                        foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                        {
                            if (creatureBase is Character)
                            {
                                Character character = (Character)creatureBase;
                                character.UpdateCreatureBaseMana(TargetCreatureBase);
                            }
                        }
                    }
                }
            }
            if (ticksLeft == 0)
            {
                RemoveSpell(true, true);
                return;
            }
            else
                spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);
        }
    }
}
